package cn.ssm.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ssm.pojo.OrderSeat;
import cn.ssm.pojo.Seat;
import cn.ssm.service.SeatService;

/**
 * 座位控制层
 * 
 * @author 黄少涛
 *
 */

// 查询影厅座位最大行数与最大列数返回座位模板与已订座位数组
@Controller
@RequestMapping("/html/seat")
public class SeatController {
	@Autowired
	private SeatService seatService;

	@RequestMapping("/getRowLineMax")
	@ResponseBody
	public Object getRowLineMax(Integer scheduleId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("cuccess", false);
		try {
			Seat seatMax = seatService.getRowLineMax(scheduleId); // 储存需要打印座位的最大列与最大行
			List<Seat> seatList = seatService.getSeatList(scheduleId);// 储存实际存在的座位
			StringBuffer seats = new StringBuffer(); // 储存打印一行的座位模板
			List<String> list = new ArrayList<String>(); // 储存所有的座位
			List<Seat> rowList = new ArrayList<Seat>(); // 储存一行的实际存在位置座位对象

			List<String> orderSeatList = seatService.getOrderSeatList(scheduleId); // 储存已订座位

			// 循环打印座位模板
			int i = 0;
			for (i = 0; i < seatMax.getRow(); i++) { // 循环行
				seats.setLength(0); // 清空StringBuffer
				rowList.clear(); // 清空一排的集合 循环储存下一行座位
				for (int k = 0; k < seatList.size(); k++) {

					if (seatList.get(k).getRow() == (i + 1)) {
						rowList.add(seatList.get(k)); // 循环添加打印行的对象到集合

					}
				}

				for (int j = 0, x = 0; j < seatMax.getLine(); j++) { // 循环列
					if (rowList.size() == 0) { // 如果此行没有座位则全部打印_
						seats.append("_");
						continue;

					} else if ((x + j) < rowList.size()) { // 如果此行有座位数据就添加打印
						int num = rowList.get(x + j).getLine();
						if (num == (j + 1)) {
							seats.append("a");

						} else {
							seats.append("_");
							x--;

						}
					} else if (rowList.size() <= seatMax.getLine()) { // 如果此行座位小于最大数则打印_占位
						seats.append("_");
						x--;
					}
				}
				list.add(seats.toString()); // 把拼接的一排座位数放到数组

			}

			map.put("cuccess", true);
			map.put("result", list); // 储存已打印好的座位模板
			map.put("result2", orderSeatList.toArray()); // 储存已订座位
			map.put("message", "查询成功");
		} catch (Exception e) {
			map.put("message", "查询出现异常");
			e.printStackTrace();
		}

		return map;

	}
	
	//删除已订座位
	@RequestMapping("/deleteOrderSeat")
	@ResponseBody
	public Object deleteOrderSeat(Integer orderId) {
		Map<String, Object> params = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("success", false);
		params.put("orderId", orderId);  //订单编号
		
		try {
			int num = seatService.deleteOrderSeat(params);
			if (num>0) {
				map.put("result", num);
				map.put("success", true);
				map.put("message", "删除已订座位成功");
			}else {
				map.put("message", "删除已订座位失败");
			}
		} catch (Exception e) {
			map.put("message", "删除已订座位出现异常，请联系管理员!");
			e.printStackTrace();
		}
		return map;
	}
	
		//修改已订座位
		@RequestMapping("/updateOrderSeat")
		@ResponseBody
		public Object updateOrderSeat(Integer orderId) {
			Map<String, Object> params1 = new HashMap<String, Object>();
			Map<String, Object> params2 = new HashMap<String, Object>();
			Map<String, Object> map = new HashMap<String, Object>();
			
			
			map.put("success", false);
			params1.put("orderId", orderId);  //订单编号
			
			try {
				int num=0;
				List<OrderSeat> orderSeats = seatService.getOrderSeat(params1);  //通过订单Id找到全部的座位信息
				
				for (OrderSeat orderSeat : orderSeats) {
					int setId = orderSeat.getSeatId();
					String orderSeatStr =  orderSeat.getOrderSeat();
					String  newOrderSeat = orderSeatStr.replace("_", "-");
					params2.put("orderId", orderId);
					params2.put("seatId", setId);
					params2.put("orderSeat", newOrderSeat);
					num = seatService.updateOrderSeat(params2);
					params2.clear();
					
				}
				
				map.put("result", num);
				if (num>0) {
					
					map.put("success", true);
					map.put("message", "修改已订座位成功");
				}else {
					map.put("message", "修改已订座位失败");
				}
			} catch (Exception e) {
				map.put("message", "修改已订座位出现异常，请联系管理员!");
				e.printStackTrace();
			}
			return map;
		}
		
		//查询订单座位关系表
		@RequestMapping("/getOrderSeat")
		@ResponseBody
		public Object getOrderSeat(Integer orderId) {
			Map<String, Object> params = new HashMap<String, Object>();
			Map<String, Object> map = new HashMap<String, Object>();
						
			map.put("success", false);
			params.put("orderId", orderId);  //订单编号
	
			try {
				List<OrderSeat> orderSeats = seatService.getOrderSeat(params);
				map.put("result", orderSeats);
				map.put("success", true);
				map.put("message", "查询订单座位关系表成功");		
				} catch (Exception e) {
					map.put("message", "查询订单座位关系表出现异常，请联系管理员!");
					e.printStackTrace();
				}
				return map;
			}	

}
